package com.matt.foundation.vo.observation;

import com.matt.foundation.DO.ChildClassesDO;
import com.matt.foundation.constant.CourseConstant;
import com.matt.foundation.vo.LiveRecordChildrenVo;
import lombok.Data;
import org.assertj.core.util.Lists;

import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/5/9
 * *******************************
 */
@Data
public class LiveRecordClassesVo {
    public static final String OTHER_NAME="其他";

    private String classesId;
    private String classesName;

    private Integer classesCategory;

    private List<LiveRecordChildrenVo> childrenVoList;

    private static LiveRecordClassesVo of(String classesName, Integer categrory, List<LiveRecordChildrenVo> list){
        LiveRecordClassesVo vo = new LiveRecordClassesVo();
        vo.setClassesName(classesName);
        vo.setClassesCategory(categrory);
        vo.setChildrenVoList(list);
        return vo;
    }

    public static List<LiveRecordClassesVo> mergeOfNursery(List<ChildClassesDO> nurseryList){
        List<LiveRecordClassesVo> list = Lists.newArrayList();
        for (ChildClassesDO o :nurseryList){
            if (containsAndFillIn(list, o)){
            }else {
                list.add(LiveRecordClassesVo.of(o.getClassesName(), CourseConstant.Category.NURSERY.codeOf(), Lists.newArrayList(LiveRecordChildrenVo.of(o))));
            }
        }
        return list;
    }

    public static List<LiveRecordClassesVo> mergeOfEarly(List<ChildClassesDO> earlyList){
        List<LiveRecordClassesVo> list = Lists.newArrayList();
        for (ChildClassesDO o :earlyList){
            if (containsAndFillIn(list, o)){
            }else {
                list.add(LiveRecordClassesVo.of(o.getClassesName(), CourseConstant.Category.EARLY.codeOf(), Lists.newArrayList(LiveRecordChildrenVo.of(o))));
            }
        }
        return list;
    }

    public static List<LiveRecordClassesVo> mergeOfNoClasses(List<ChildClassesDO> noClassesList){
        List<LiveRecordClassesVo> list = Lists.newArrayList();
        for (ChildClassesDO o :noClassesList){
                o.setClassesName(OTHER_NAME);
            if (containsAndFillIn(list, o)){
            }else {
                list.add(LiveRecordClassesVo.of(o.getClassesName(), -1, Lists.newArrayList(LiveRecordChildrenVo.of(o))));
            }
        }
        return list;
    }

    private static boolean containsAndFillIn(List<LiveRecordClassesVo> list, ChildClassesDO childClassesDO){
        for (LiveRecordClassesVo o :list){
            if (o.getClassesName().equals(childClassesDO.getClassesName())){
                o.getChildrenVoList().add(LiveRecordChildrenVo.of(childClassesDO));
                return true;
            }
        }
        return false;
    }
}
